package com.tangyee.home_knowledge.acticity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tangyee.R;
import com.tangyee.home_knowledge.bean.KnowledgeSortExpandItem;
import com.tangyee.home_knowledge.util.JsoupFilterHTML;
import com.tangyee.home_knowledge.view.ProgressWebView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class KnowledgeSortItemDetailActivity extends AppCompatActivity {

    private String titleString;
    private KnowledgeSortExpandItem knowledgeSortExpandItem;
    private ProgressWebView progressWebView;
    private LinearLayout backView;
    private TextView titleView;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticity_knowledge_detail);

        // 得到传来的值
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        knowledgeSortExpandItem = (KnowledgeSortExpandItem) bundle.getSerializable("itemContent");
        titleString = knowledgeSortExpandItem.getKnowledge().getTitle();

        initDetailPageTitle();

        progressWebView = (ProgressWebView) findViewById(R.id.webview);
        Document document = Jsoup.parse(getHtmlData(knowledgeSortExpandItem));
        // 使用 Jsoup 对指定标签的 Style 进行过滤
        JsoupFilterHTML.jsoupFilterStyle(document, "p>span", "white-space", "normal");
        progressWebView.loadDataWithBaseURL(null, document.toString(), "text/html", "utf-8", null);
    }

    private void initDetailPageTitle(){
        backView = (LinearLayout) findViewById(R.id.knowledge_detail_back);
        titleView = (TextView) findViewById(R.id.knowledge_detail_title);

        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        titleView.setText(titleString);
    }

    /**
     * 加载html标签
     *
     * @param knowledgeSortExpandItem
     * @return
     */
    private String getHtmlData(KnowledgeSortExpandItem knowledgeSortExpandItem) {
        String head =
                "<head>" +
                        "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                        "<style>img{max-width: 100%; width:auto; height:auto!important;}</style>" +
                        "<style>" +
                        ".pb-mt15{margin-top:15px; <!--text-align: right; margin-right:10px;--> margin-bottom:15px; <!--margin-left:20px;-->}" +
                        ".pb-size-tiny{font-size:12px;}" +
                        ".meta{line-height:12px}" +
                        ".at{margin-left:15px;}" +
                        ".vw i{background-position:0 0 }" +
                        "" +
                        "</style>" +
                        "</head>";
        return "<html>" +
                head +
                "<body style=\"word-wrap:break-word; background:#F3F3F3; \">" +
                "<div style=\"clear: both; content: .; display: block;\">" +
                "<h3 style=\"border-left: thick solid #21aa8e; color:#21aa8e; padding-left:10px; margin-bottom:15px; margin-top:15px; \">" +
                knowledgeSortExpandItem.getKnowledge().getTitle() +
                "</h3>" +
                "</div>" +
                "<div class=\"pb-mt15 pb-size-tiny meta\" style=\"margin-bottom:15px;\">" +
                "<span class=\"dt\">" +
                "&nbsp;&nbsp;" + knowledgeSortExpandItem.getKnowledge().getRealYearMonthDate() +
                "</span>" +
                "<span class=\"at\">发布者：" +
                "<!--<a href=\"/Web/DoctorInfo/36\">-->" + knowledgeSortExpandItem.getKnowledge().getAuthorID() + "<!--</a>-->" +
                "</span>" +
                "&nbsp;&nbsp;|&nbsp;&nbsp;" +
                "<span class=\"vw\"><i class=\"pb-icons\"></i>" +
                "浏览" + "(" + "null" + ")"  +
                "</span> " +
                "&nbsp;&nbsp;|&nbsp;&nbsp;" +
                "</div>" +
                knowledgeSortExpandItem.getKnowledge().getArticleContent() +
                "</body>" +
                "</html>";
    }

}
